package com.LeetCode.Greedy;

import org.junit.Test;

/**
 * 分糖果
 */
public class SplitCandy {

    public int candy(int[] ratings) {
        if(ratings == null || ratings.length == 0)
            return 0;

        int len = ratings.length;
        int[] dp = new int[len];
        dp[0] = 1;
        for(int i = 1; i<len; i++){
            if(ratings[i] > ratings[i-1]){
                dp[i] = dp[i-1] + 1;
            }else{
                dp[i] = 1;
            }
        }

        for(int i = len - 2; i >=0; i--){
            if(ratings[i] > ratings[i+1] && dp[i] <= dp[i+1]){
                dp[i]  = dp[i+1] + 1;
            }
        }

        int count = 0;
        for(int i : dp){
            count += i;
        }
        return count;
    }

    @Test
    public void test(){
        int[] ratings = {1,2,2};
        System.out.println(candy(ratings));
    }
}
